import numpy as np
from scipy.interpolate import RBFInterpolator
# matplotlib.use('Agg') # non active backends
import matplotlib.pyplot as plt
from matplotlib import cm

# 2-d tests - setup scattered data
x = np.random.rand(100) * 4.0 - 2.0
y = np.random.rand(100) * 4.0 - 2.0
xy = np.column_stack((x, y))  # 给定点 sources
z = x * np.exp(-x**2 - y**2)

#  [x,y] ... 采样点网格
xgrid = np.mgrid[-2.0:2.0:100j, -2.0:2.0:100j]
xflat = xgrid.reshape(2, -1).T
# use RBF
rbf = RBFInterpolator(xy, z, epsilon=2)
zflat = rbf(xflat)
zgrid = zflat.reshape(100, 100)

# plot the result
n = plt.Normalize(-2., 2.)
plt.subplot(1, 1, 1)
plt.pcolormesh(*xgrid, zgrid, cmap=cm.jet)
plt.scatter(x, y, 100, z, cmap=cm.jet)
plt.title('RBF interpolation - multiquadrics')
plt.xlim(-2, 2)
plt.ylim(-2, 2)
plt.colorbar()
# plt.savefig('rbf2d.png') # non active backends
plt.show()
